﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using CharacterTrailClient.Core;
using CharacterTrailClient.Windows;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Security;
using CharacterTrailLibrary.StaticData;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;

namespace CharacterTrailClient
{
    public partial class FormMain : XtraForm
    {
        private IUserLoader _loader = InstanceFactory.GetUserLoader();
        private const string HeadCaption ="【生产管理】用户ID：{0}，用户名：{1}";
        private readonly Thread _threadLoadStaticData = null;
        public FormMain()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            _threadLoadStaticData = new Thread(StaticObjects.RefreshStaticObjects) {IsBackground = true};
            _threadLoadStaticData.Start();
            if (!AppInitializer.UsingGridStyleSaving)
            {
                this.barButtonItemClearGridStyle.Visibility = BarItemVisibility.Never;
            }
// ReSharper disable DoNotCallOverridableMethodsInConstructor
            this.Text = string.Format(HeadCaption, UserSession.CurrentUser.UserId, UserSession.CurrentUser.UserName);
// ReSharper restore DoNotCallOverridableMethodsInConstructor
            RoleControl();
        }

        private void RoleControl()
        {
            if (!_loader.GetRoleByIndex(1))
            {
                this.barButtonItemProcedure.Enabled = false;
            }

            if (!_loader.GetRoleByIndex(2))
            {
                this.barSubItemBomManage.Enabled = false;
            }

            if (!_loader.GetRoleByIndex(3))
            {
                this.barSubItemGoodsCodeType.Enabled = false;
            }

            if (!_loader.GetRoleByIndex(4))
            {
                this.barButtonItemRepair.Enabled = false;
            }

            if (!_loader.GetRoleByIndex(5))
            {
                this.barButtonItemTracking.Enabled = false;
            }
        }


        private void BarSubItemBomManageItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //MainFormControl.GenerateForm<FormBom>(this);
            MainFormControl.GenerateForm<FormBomMain>(this);
        }

        private void BarSubItemGoodsCodeTypeItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            MainFormControl.GenerateForm<FormRelation>(this);
        }

        private void BarSubItemQuitItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Application.Exit();
        }

        private void BarButtonItemProcedureItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            MainFormControl.GenerateForm<FormProcedure>(this);
        }

        private void BarButtonItemRepairItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            MainFormControl.GenerateForm<FormRepairMaintain>(this);
        }

        private void BarButtonItemTrackingItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            MainFormControl.GenerateForm<FormSequenceTracking>(this);
        }

        private void BarButtonItemClearGridStyleItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                string dir = Application.StartupPath + "\\Layout";
                if (Directory.Exists(dir))
                {
                    if (Message.MsgBox.ShowYesOrNo("确实要清除Grid的样式文件吗？"))
                    {
                        Directory.Delete(dir,true);
                        Message.MsgBox.ShowInfomation("清除完成！");
                    }
                }
            }
            catch (Exception exception)
            {
                Message.MsgBox.ShowError(exception.Message);
            }
        }

        private void BarButtonItemComponentScanItemClick(object sender, ItemClickEventArgs e)
        {
            MainFormControl.GenerateForm<FormComponentScan>(this);
        }
    }
}
